#include <logbase.h>
#include <hexstr.h>

#define LOG_OUT_LEVEL_MAX logLevelTrace
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
int clogCommon(int lev, const char *lfile, int lline, const char *fmt, ...)
{
    if (lev > LOG_OUT_LEVEL_MAX)
    {
        return 0;
    }
    static const char *logstr[] = {
        "fat",
        "err",
        "wrn",
        "inf",
        "dbg",
        "trc"};
    struct timeval tnow;
    struct tm *tmnow;
    gettimeofday(&tnow, NULL);
    tmnow = localtime(&tnow.tv_sec);
    char tmpPrtStr[2048] = {0};
    const char *tmpFileName;
    (tmpFileName = strrchr(lfile, '/')) ? tmpFileName++ : (tmpFileName = lfile);
    int tmplen = snprintf(tmpPrtStr, sizeof(tmpPrtStr), "[%d-%02d-%02d %02d:%02d:%02d.%03d]%s[%s,%d]",
                          tmnow->tm_year + 1900, tmnow->tm_mon + 1, tmnow->tm_mday, tmnow->tm_hour, tmnow->tm_min, tmnow->tm_sec,
                          (int)(tnow.tv_usec / 1000 % 1000), logstr[lev], tmpFileName, lline);
    va_list args;
    va_start(args, fmt);
    tmplen += vsnprintf(tmpPrtStr + tmplen, sizeof(tmpPrtStr) - tmplen, fmt, args);
    va_end(args);
    printf("%s\n", tmpPrtStr);
    return tmplen;
}
#elif DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_ARM32_MCU
int clogCommon(int lev, const char *lfile, int lline, const char *fmt, ...)
{
    if (lev > LOG_OUT_LEVEL_MAX)
    {
        return 0;
    }
    char tmpPrtStr[256] = {0};
    const char *tmpFileName;
    (tmpFileName = strrchr(lfile, '/')) ? tmpFileName++ : (tmpFileName = lfile);
    int tmplen = snprintf(tmpPrtStr, sizeof(tmpPrtStr), "[%s,%d]", tmpFileName, lline);
    va_list args;
    va_start(args, fmt);
    tmplen += vsnprintf(tmpPrtStr + tmplen, sizeof(tmpPrtStr) - tmplen, fmt, args);
    va_end(args);
    printf("%s\n", tmpPrtStr);
    return tmplen;
}
#elif DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_C51_MCU
void uartSndByte_1(uint8_t byte);
void logSndByte(char sndval)
{
    if (sndval == '\n')
    {
        uartSndByte_1('\r');
    }
    uartSndByte_1(sndval);
}
void logSndStr(const char *sndStr, int sndlen)
{
    int i = 0;
    for (i = 0; i < sndlen; i++)
    {
        logSndByte(sndStr[i]);
    }
}
// void logfatalc(const char *fmt, ...)
// {
//     fmt = NULL;
// }
// void logerrorc(const char *fmt, ...)
// {
//     fmt = NULL;
// }
// void logwarnc(const char *fmt, ...)
// {
//     fmt = NULL;
// }
// void loginfoc(const char *fmt, ...)
// {
//     fmt = NULL;
// }
void logdebugc(const char *fmt, ...)
{
    // for c51
    char DEV_C51_XDATA_FLAG tmpbuf[128];
    va_list DEV_C51_XDATA_FLAG vArgList;
    va_start(vArgList, fmt);
    vsprintf(tmpbuf, fmt, vArgList);
    va_end(vArgList);
    logSndStr(tmpbuf, strlen(tmpbuf));
    logSndByte('\n');
}
static void logSndHexAsc(unsigned char byte)
{
    char tmpbuf[2] = {'0', '0'};
    cvtHex2str(&byte, 1, tmpbuf, 2);
    logSndByte(tmpbuf[0]);
    logSndByte(tmpbuf[1]);
}
#endif
void logHexBufStr(const char *expStr, const char *hexptr, int hexsize)
{
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    char tmpHexStr[1025];
    cvtHex2str(hexptr, hexsize, tmpHexStr, sizeof(tmpHexStr));
    printf("%s:%s\n", expStr, tmpHexStr);
#elif DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_ARM32_MCU
    char tmpHexStr[129];
#else
    int DEV_C51_XDATA_FLAG i = 0;
    while (*expStr != '\0')
    {
        logSndByte((unsigned char)*expStr++);
    }
    for (i = 0; i < hexsize; i++)
    {
        logSndHexAsc(*hexptr++);
    }
    logSndByte('\n');
#endif
}
